/**
 * ipassbook 0.6.0
 * Copyright (C)2009 wateray and contributors
 * wateray@gmail.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package com.wateray.ipassbook.kernel.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import com.ibatis.dao.client.DaoManager;
import com.wateray.ipassbook.commom.Constant;
import com.wateray.ipassbook.domain.Subject;
import com.wateray.ipassbook.persistence.DaoConfig;
import com.wateray.ipassbook.persistence.iface.SubjectDao;

/**
 * @author wateray
 * @create 2009-5-23
 */
public class SubjectService {

	private SubjectDao subjectDao;

	public SubjectService() {
		DaoManager daoMgr = DaoConfig.getDaoManager();
		this.subjectDao = (SubjectDao) daoMgr.getDao(SubjectDao.class);
	}

	public SubjectService(SubjectDao subjectDao) {
		this.subjectDao = subjectDao;
	}

	public Subject getSubject(String subjectId) {
		return subjectDao.getSubjectBySubjectId(subjectId);
	}
	

	public List<Subject> getAllSubjectList() {
		return subjectDao.getAllSubjectList();
	}

	public List<Subject> getSubjectListBySubjectFlag(String subjectFlag) {
		return subjectDao.getSubjectListBySubjectFlag(subjectFlag);
	}

	public List<Subject> getSubjectListByParentId(String parentId) {
		return subjectDao.getSubjectListByParentId(parentId);
	}

	/**
	 * @param subjectFlag
	 *            .
	 * @return parents and children list.
	 * */
	public Vector<Subject> getParentAndChildrenListByFlag(String subjectFlag) {
		Vector<Subject> returnList = new Vector<Subject>();
		List<Subject> subjectList;
		subjectList = subjectDao.getSubjectListBySubjectFlag(subjectFlag);
		Iterator<Subject> e = subjectList.iterator();
		while (e.hasNext()) {
			Subject subject = (Subject) e.next();
			// add to returnList.
			returnList.add(subject);
			Iterator<Subject> c = subject.getChildrenList().iterator();
			while (c.hasNext()) {
				Subject child = (Subject) c.next();
				// add child to returnList.
				returnList.add(child);
			}
		}
		return returnList;
	}

	public int insertSubject(Subject subject) {
		subject.setSubjectId(fillZero(subjectDao.getMaxId(),6));
		
		if(subject.getParentId() != null){
			subject.setViewOrder(subjectDao.getMaxOrderOfParent(subject.getParentId()) + 1);
		}
		else{
			//parentId = subjectId
			subject.setParentId(subject.getSubjectId());
			subject.setViewOrder(subjectDao.getMaxOrderOfFlag(subject.getSubjectFlag())+ 1);	
		}
		
		return subjectDao.insertSubject(subject);
	}

	public int updateSubject(Subject subject) {
		return subjectDao.updateSubject(subject);
	}

	public int deleteSubject(Subject subject) {
		return subjectDao.deleteSubject(subject);
	}

	private String fillZero(String value, int bit) {
		String zeros = "";
		value = value==null?"0" : value.trim().equals("")?"0" :value;
		// +1
		value = String.valueOf(Integer.parseInt(value) + 1);
		
		if (value.length() < bit) {
			for (int i = 0; i < bit; i++) {
				zeros += "0";
			}

			value = zeros.substring(value.length()) + value;
		}
		return value;
	}

	public static void main(String[] args) {
		SubjectService service = new SubjectService();
//		List<Subject> subjectList;
//		subjectList = service.getSubjectListBySubjectFlag(Constant.EXPENSE_KBN);
//		// subjectList = service.getSubjectListByParentId("000001");
//		Iterator<Subject> e = subjectList.iterator();
//		while (e.hasNext()) {
//			Subject subject = (Subject) e.next();
//			System.out.println(" ParentId=" + subject.getParentId()
//					+ " SubjectId=" + subject.getSubjectId() + "  "
//					+ subject.getName());
//			Iterator<Subject> c = subject.getChildrenList().iterator();
//			while (c.hasNext()) {
//				Subject child = (Subject) c.next();
//				System.out.println(" ----ParentId=" + child.getParentId()
//						+ " SubjectId=" + child.getSubjectId() + "  "
//						+ child.getName());
//			}
//		}
		service.getAllSubjectList();
	}
}
